/**
 * [pageDes application.js]
 * @description  主控制器
 * @return {[type]}      [description]
 */
var path = require( 'path' ),
url      = require( 'url' );

module.exports = function( oApp ){
    
    let app      =   oApp.app,            // App对象,即整个应用程序对象
        Config   =   app.get('config'),   // 一些web配置信息
        psfx     =   Config.psfx,         // 页面访问的扩展/后缀名 .html
        isfx     =   Config.isfx,         // 接口访问的扩展/后缀名 .json
        helper   =   oApp.helper,         // 辅助函数库
        proxy    =   oApp.proxy;          // http 服务代理
    
    app.all('*', function( request, response, next ){
        next();
    })
    
    // 公有或者根拦截器就放这里文件这边
    app.use('/', function( request, response, next ){
        
        if( request.url=='' || request.url=='/' ){
            response.redirect( '/index.html' );
            return false;
        }
        
        var pagename = path.parse( request.url ).name,
        pathname = url.parse( request.url ).pathname,
        extname = path.extname( pathname );
        
        // 文件名的输出
        app.locals.pageName = pagename;
        app.locals.params = request.query;
        
        if( (extname=='.html' || extname=='.json') && pathname.search(/(\/login.html|\/userLogin.json)/g)==-1 && (!request.session.user||!request.session.user.token) ){
            
            extname=='.json' ? response.send({success: false, errorCode: 2001, message: "连接超时,请重新登录"}) : response.redirect( '/login.html' );
            return false;
            
        }
        
        next();
        
    });
    
}